package ar.com.tesis.mobile.aplicacion;

import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;

import org.apache.http.HttpResponse;
import org.apache.http.HttpVersion;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.security.KeyStore;
import java.util.ArrayList;
import java.util.List;

import ar.com.tesis.mobile.model.FotoPuntoDeInteres;
import ar.com.tesis.mobile.model.PuntoDeInteres;

public class TesisApi {

	private static final String LOG_TAG = TesisApi.class.getSimpleName();

	private TesisApi() {
	}

	// URL Testing
	//private static String apiURL = "http://192.168.1.104:8080/tesis-server/mobile/";
    private static String apiURL = "https://tesis-server.herokuapp.com/mobile/";

	public static List<PuntoDeInteres> obtenerPuntosDeInteres() throws IOException {

        HttpGet httpGet = new HttpGet(apiURL + "puntosDeInteres");
        httpGet.setHeader("Content-type", "application/json");
        List<PuntoDeInteres> puntosDeInteres = new ArrayList<PuntoDeInteres>();

        HttpResponse httpResponse = getNewHttpClient().execute(httpGet);

        int responseStatus = httpResponse.getStatusLine().getStatusCode();
        String response = EntityUtils.toString(httpResponse.getEntity());

        if (responseStatus / 200 == 1) {

            Log.i(LOG_TAG, "Get Punto De Interes Response: " + response + " Status: "
                    + responseStatus);

            Gson gson = new GsonBuilder().serializeNulls().create();
            puntosDeInteres = gson.fromJson(response, new TypeToken<List<PuntoDeInteres>>() {
            }.getType());

        } else {

            Log.i(LOG_TAG, "Error. Response " + response + " Status: "
                    + responseStatus);
        }

        return puntosDeInteres;
    }

    public static List<FotoPuntoDeInteres> obtenerFotosDelPuntoDeInteres(Integer idPuntoDeInteres) throws IOException {

        HttpGet httpGet = new HttpGet(apiURL + "puntosDeInteres/"+ idPuntoDeInteres+"/fotos");
        httpGet.setHeader("Content-type", "application/json");
        List<FotoPuntoDeInteres> fotosPuntoDeInteres = new ArrayList<FotoPuntoDeInteres>();

        HttpResponse httpResponse = getNewHttpClient().execute(httpGet);

        int responseStatus = httpResponse.getStatusLine().getStatusCode();
        String response = EntityUtils.toString(httpResponse.getEntity());

        if (responseStatus / 200 == 1) {

            Log.i(LOG_TAG, "Get Fotos del Punto De Interes Response: " + response + " Status: "
                    + responseStatus);

            Gson gson = new GsonBuilder().serializeNulls().create();
            fotosPuntoDeInteres = gson.fromJson(response, new TypeToken<List<FotoPuntoDeInteres>>() {
            }.getType());

        } else {

            Log.i(LOG_TAG, "Error. Response " + response + " Status: "
                    + responseStatus);
        }

        return fotosPuntoDeInteres;
    }
	
	private static HttpClient getNewHttpClient() {

		try {

			KeyStore trustStore = KeyStore.getInstance(KeyStore
					.getDefaultType());
			trustStore.load(null, null);

			SSLSocketFactory sf = new MySSLSocketFactory(trustStore);
			sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);

			HttpParams params = new BasicHttpParams();
			HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
			HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);
			HttpConnectionParams.setSoTimeout(params, 15000);
			HttpConnectionParams.setConnectionTimeout(params, 15000);

			SchemeRegistry registry = new SchemeRegistry();
			registry.register(new Scheme("http", PlainSocketFactory
					.getSocketFactory(), 80));
			registry.register(new Scheme("https", sf, 443));

			ClientConnectionManager ccm = new ThreadSafeClientConnManager(
					params, registry);

			return new DefaultHttpClient(ccm, params);

		} catch (Exception e) {
			return new DefaultHttpClient();
		}
	}
}
